package string;

import com.google.common.base.Charsets;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.junit.Test;

import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Base64;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringDemo {

  public static void main(String[] args) {

    int a =  -2147483648;
    a = Integer.MIN_VALUE;
    System.out.println(Integer.MIN_VALUE);
    System.out.println(Integer.MAX_VALUE);

  }

  @Test
  public void test1() {



    String ll = "|bb|aa|cc|";
    char c = ll.charAt(1);
    boolean x = '|' == c;
    if (x)
      System.out.println(x);
    ll = ll.substring(1, ll.length() - 1);
    System.out.println(Arrays.toString(ll.split("\\|", 0)));
    System.out.println(drstr("\\XA81"));
    System.out.println((int) '\\');
    System.out.println(new Date(1591928336314L));

  }

  public static String drstr(String rstr) {
    byte[] bs = rstr.getBytes();
    ByteBuffer buf = ByteBuffer.allocate(bs.length);
    for (int i = 0; i < bs.length; i++) {
      if (bs[i] == 92 && bs[i + 1] == 120) {
        Integer in = Integer.valueOf((char) bs[i + 2] + "" + (char) bs[i + 3], 16);
        in = in & 0xFF;
        buf.put(in.byteValue());
        i += 3;
      } else {
        buf.put(bs[i]);
      }
    }
    return new String(buf.array()).trim();
  }

  //余弦相似度
  @Test
  public void test3() {
    String matchWord = "巴黎到火";
    String word = "巴黎到巴登巴登的火车票";
    sim(matchWord, word);
    String matchWord1 = "巴黎到火";
    String word1 = "巴黎到伦敦的火车票";
    sim(matchWord1, word1);
  }

  public void sim(String matchWord, String word) {
    Map<Character, Integer> characters = Maps.newHashMap();
    char[] charArr1 = matchWord.toCharArray();
    for (int i = 0; i < charArr1.length; i++) {
      characters.put(charArr1[i], i);
    }
    int simNum = 0;
    char[] charArr2 = word.toCharArray();
    for (int i = 0; i < charArr2.length; i++) {
      char c = charArr2[i];
      if (characters.containsKey(c)) {
        simNum += 10 - Math.abs(i - characters.get(c));
      }
    }
    System.out.println(simNum);
  }

  //小写
  @Test
  public void test4() {
    System.out.println(("PKID\n" +
            "id\n" +
            "Type\n" +
            "word\n" +
            "multikeyword\n" +
            "rankscore\n" +
            "imageurl\n" +
            "CommentCount\n" +
            "lat\n" +
            "lon\n" +
            "districtid\n" +
            "datachange_lasttime").toLowerCase());
  }

  @Test
  public void test11() {
    String str = "hotel,district,sight,ticket,visa,travelgroup,flighthotel,sighthotel,hhtravel,huodong,airport,flightline,trainline," +
            "busline,travelgroupline,flighthotelline,tourline,trafficline,carline,flightandtourline,shuttle,delivery,pickup,toursline,hotelbrand,productline,tuan,shop,globalshopstore,globalshopbrand,cruise,flightnum,trainnum,trainflightnum,shipline,districtvisa,charters,hoteldistrict,hotellist,districthotel,hotelgroup,charter,tourguide,channelsport,headline,trainpass,multiproduct,flightairport,nosalehotel,lounge,wifi,yacht,healthcare,hotplaylist,author,inngeolocation,innsight,ranklist,inn,config,advertise,inncity,inncityandsight,classictravel,innroom,tripshoot,store,poiranklist,tripshoottopic,airportresource,gspocket,journey,snack,trainstation,insurance,bigbusstation,currency,specialty,topic,metro,dingzhi,education,sightfood,traveler,marketadver,location,flightspecialty,hoteltuan,restaurant,cityrentalcar,rentalcar,gsmarkland,markland";
    String[] split = str.split(",");

    String str1 = "travelnotes,gsask,headline,tripshoot,topic,traveler,gspocket,journey,poiranklist,author," +
            "restaurant,gsmarkland,sight,shop,trainstation,bigbusstation,airport,sightplay,ticket,district," +
            "markland,hotel,hotelbrand,tuan,hoteldistrict,zone,location,city,province,hotelgroup,metro,hoteltuan,nosalehotel,inn,inncity,innsight,innzone,innlocation,innmarkland ,innroomtype ,innroomlandscape," +
            "visa,travelgroup,flighthotel,hhtravel,huodong,tourguide,dingzhi," +
            "flightline,trainline,busline,travelgroupline,flighthotelline,tourline,carline,shipline,flightnum,trainnum," +
            "shuttle,delivery,pickup,cruise,districtvisa,citycharter,citylinetour,healthcare,trainpass,flightairport,lounge/securitychannel,yacht,config,snack,insurance,currency,specialty,sightfood,marketadver,flightspecialty,cityrentalcar,rentalcar";

    String[] split1 = str1.split(",");
    List<String> exit = Lists.newArrayList();
    for (String s : split1) {
      exit.add(s.trim());
    }

    for (String s : split) {
      if (!exit.contains(s)) {
        System.out.println(s);
      }
    }


  }

  //正则表达式提取
  @Test
  public void test() {
    Pattern pattern = Pattern.compile("Time-Used:\\d+ms");
    String str = "connection:close;Time-Used:7ms;";
    Matcher matcher = pattern.matcher(str);
    while (matcher.find()) {
      System.out.println(matcher.group());
    }
    String[] split = str.split(";");
    String s = split[split.length - 1];
    System.out.println(s);
    Pattern pattern2 = Pattern.compile("\\d+");
    Matcher matcher2 = pattern2.matcher(s);
    while (matcher2.find()) {
      System.out.println(matcher2.group());
    }

  }

  //StringBuilder中的length用法
  @Test
  public void test5() {
    String a = "12,4,,";
    StringBuilder stringBuilder = new StringBuilder(a);
    for (; stringBuilder.length() > 5; ) {
      int i = stringBuilder.lastIndexOf(",");
      stringBuilder.setLength(i);
    }
    System.out.println(stringBuilder);
  }

  @Test
  public void test6() {
    Pattern compile = Pattern.compile("utmsource=[\\w]+");
    String url = "ctrip://wireless/h5?page=index.html#yacht/yacht/list&path=yacht&utmsource=dasou5&city=%e4%b8%89%e4%ba%9a";
    Matcher matcher = compile.matcher(url);
    boolean b = matcher.find();
    System.out.println(b);
    String tag = matcher.replaceFirst("utmsource=杜读");
    System.out.println(tag);
  }


}
